package com.example.mydemo103.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mydemo103.Adapter.ListViewAdapter;
import com.example.mydemo103.MainActivity;
import com.example.mydemo103.R;
import com.example.mydemo103.bean.Book;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BookSerchActivity extends AppCompatActivity {
    private LinearLayout bookallbylinertlayout;
    private ImageView bookallbyleft;
    private List<Book> booklist=new ArrayList<>();
    private final static String HTML_URL = "http://192.168.17.1:8080/libraryMs_war/book/book/getBooksByTitle.do?title=";
    private TextView author,author1;
    OkHttpClient okHttpClient = new OkHttpClient();
    private Handler handler = new Handler() {
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    try {
                        JSONObject jsonObject1 = new JSONObject(msg.obj.toString());
                        System.out.println(msg.obj.toString()+"URL");
                        JSONArray jsonArray = jsonObject1.getJSONArray("book");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                            //取出name
                            String bookname = jsonObject.getString("title");
                            String bookauthor = jsonObject.getString("author");
                            String booktext = jsonObject.getString("type");
                            Book book=new Book(bookname,bookauthor,booktext);
                            booklist.add(book);
                            ListViewAdapter listViewAdapter=new ListViewAdapter(BookSerchActivity.this,R.layout.book_layout,booklist);
                            ListView listView=findViewById(R.id.serchbooklist);
                            listView.setAdapter(listViewAdapter);
//                    JSONArray jarray1 = jsonObject.getJSONArray("author");
//                    JSONArray jarray2 = jsonObject.getJSONArray("type");
                            Log.e("Json", bookname);
                            System.out.println(bookauthor);
                            System.out.println(booktext);
                        }
                        System.out.println(booklist);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serchforbook);
        final String bookname= HTML_URL+String.valueOf(getIntent().getStringExtra("bookname"));
        System.out.println(bookname);
        new Thread(){
            @Override
            public void run() {
                super.run();
                Request request = new Request.Builder().get().url(bookname).build();

                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        System.out.println("onFailure");
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        System.out.println("onResponse: ");
                        String content = response.body().string();
                        Message msg = new Message();
                        msg.obj = content;
                        msg.what=1;
                        handler.sendMessage(msg);
                    }
                });
            }
        }.start();
        author=findViewById(R.id.author);
        author1=findViewById(R.id.author1);

//        绑定当前View界面的incule Id
        bookallbylinertlayout=findViewById(R.id.serchbookbyincule);
//        绑定incule中的控件Id
        bookallbyleft=bookallbylinertlayout.findViewById(R.id.serchbookleft);
//        设置点击事件
        bookallbyleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BookSerchActivity.this, MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
            }
        });









//    去除标题栏
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
    }
    public void AllBook(){
        for(int i=0;i<10;i++){
            Book bookone=new Book("《天行剑》","余维华","正文");
            booklist.add(bookone);
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}